package com.zhongge.code;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @ClassName Demo03
 * @Description TODO
 * @Author 笨忠
 * @Date 2025/9/22 23:53
 * @Version 1.0
 */
public class Demo03 {
    public static void main(String[] args) {
        int min = 10_0000;

        Scanner in = new Scanner(System.in);
        //输入一个整数
        int n = in.nextInt();
        //输入两个个字符串
        String str1 = in.next();
        String str2 = in.next();
        String[] strs = new String[n];
        //输入多个字符串用于存储在strs中
        for (int i = 0; i < n; i++) {
            strs[i] = in.next();
        }

        //我试一下使用集合将下标存起来
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        //遍历字符串数组
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].contains(str1)) {
                list1.add(i);
            }
        }
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].contains(str2)) {
                list2.add(i);
            }
        }
        if (list1.isEmpty() || list2.isEmpty()) {
            min = -1;
        }

        for (int i = 0; i < list1.size(); i++) {
            int i1 = list1.get(i);
            for (int j = 0; j < list2.size(); j++) {
                int i2 = list2.get(j);
                if (Math.abs(i1 - i2) < min){
                    min = Math.abs(i1 - i2);
                }
            }
        }
        System.out.println(min);
    }
}
